/**
 * Copyright 2012 Roger Cockshutt
 * krakenbrau@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.parallelrl.util;

/**
 * @author Roger Cockshutt
 *
 * A CoordinatePair is a pair of coordinates. eg. size, position, etc.
 */
@Deprecated
public class CoordinatePair extends ImmutablePair<Integer, Integer> implements Comparable<CoordinatePair> {
	
	public static CoordinatePair up = new CoordinatePair(-1, 0);
	public static CoordinatePair down = new CoordinatePair(1, 0);
	public static CoordinatePair left = new CoordinatePair(0, -1);
	public static CoordinatePair right = new CoordinatePair(0, 1);
	public static CoordinatePair zero = new CoordinatePair(0, 0);

	/**
	 * @param elementA
	 * @param elementB
	 */
	public CoordinatePair(Integer y, Integer x) {
		super(y, x);
	}
	
	
	
	/**
	 * @return
	 */
	public int getY() {
		return a.intValue();
	}
	
	
	
	/**
	 * @return
	 */
	public int getX() {
		return b.intValue();
	}
	
	public CoordinatePair sum(CoordinatePair operand) {
		return new CoordinatePair(this.getY() + operand.getY(), this.getX() + operand.getX());
	}
	
	/**
	 * Output is of the form Y,X
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return a.toString() + "," + b.toString();
	}
	
	@Override
	public boolean equals(Object anobject) {
		if (anobject instanceof CoordinatePair) {
			CoordinatePair acp = (CoordinatePair) anobject;
			if (acp.getY() == this.getY() && acp.getX() == this.getX()) {
				return true;
			}
		}
		return false;
	}
	
	public Double distanceTo(CoordinatePair pos2) {
		Double dist = null;
		if (pos2 != null) {
			Double yDist = (double) (pos2.getY() - getY());
			Double xDist = (double) (pos2.getX() - getX());
			dist = Math.sqrt(Math.pow(yDist, 2) + Math.pow(xDist, 2));
		}
		return dist;
	}
	
	public static CoordinatePair valueOf(String s) {
		Integer y, x;
		y = null;
		x = null;
		String[] splitPos = s.split(",");
		if (splitPos.length >= 2) {
			y = Integer.valueOf(splitPos[0].trim());
			x = Integer.valueOf(splitPos[1].trim());
			
		}
		if (!(y == null || x == null)) {
			return new CoordinatePair(y, x);
		}
		return null;
	}
	
	public static CoordinatePair randomDirection() {
		int y = MersenneTwister.getNext(3) - 1;
		int x = MersenneTwister.getNext(3) - 1;
		return new CoordinatePair(y, x);
	}
	
	@Override
	public CoordinatePair clone() {
		return new CoordinatePair(a, b);
	}

	/* (non-Javadoc)
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	@Override
	public int compareTo(CoordinatePair o) {
		Double vThis, vThat;
		vThis = Math.sqrt(Math.pow(getY(), 2) + Math.pow(getX(), 2));
		vThat = Math.sqrt(Math.pow(o.getY(), 2) + Math.pow(o.getX(), 2));
		Integer diff = Double.valueOf(vThat - vThis).intValue();
		return diff;
	}

}
